package com.jie.store.impl;

import com.jie.dto.UploadImageDTO;
import com.jie.exception.BizException;
import com.jie.store.UploadStrategy;
import com.jie.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import static com.jie.enums.UploadModeEnum.getStrategy;

/**
 * 抽象上传模板
 *
 * @author ican
 */
@Slf4j
@Service
public abstract class AbstractUploadStrategyImpl implements UploadStrategy {

    @Override
    public String uploadFile(MultipartFile file) {
        try {
            return upload(file);
        } catch (Exception e) {
            log.info("uploadFile fail, error is {}", e.getMessage());
            throw new BizException("文件上传失败");
        }
    }

    @Override
    public UploadImageDTO uploadFileImages(MultipartFile file) {
        try {
            return uploadImages(file);
        } catch (Exception e) {
            log.info("uploadFile fail, error is {}", e.getMessage());
            throw new BizException("图片上传失败");
        }
    }

    @Override
    public String uploadFileVideo(MultipartFile file) {
        try {
            return uploadVideo(file);
        } catch (Exception e) {
            log.info("uploadFile fail, error is {}", e.getMessage());
            throw new BizException("视频上传失败");
        }
    }

    @Override
    public boolean removeFileName(String url,String fileName) {
        try {
            return removeFile(url,fileName);
        } catch (Exception e) {
            log.info("remove fail, error is {}", e.getMessage());
            throw new BizException("文件删除失败");
        }
    }
    /**
     * 上传
     * @param inputStream 输入流
     * @throws IOException io异常
     */
    public abstract String upload(MultipartFile inputStream) throws IOException;

    public abstract UploadImageDTO uploadImages(MultipartFile file);

    public abstract String uploadVideo(MultipartFile file);

    public abstract boolean removeFile(String url,String fileName);

}